package data;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class StockPriceSaleAndBuy {
	
	private Map<BigDecimal, Integer> listNotDeal = new TreeMap<BigDecimal, Integer>(new Comparator<BigDecimal>() {
		@Override
		public int compare(BigDecimal o1, BigDecimal o2) {
			return o2.compareTo(o1);
		}
	});
	private List<DealedPoint> listDealed = new ArrayList<DealedPoint>();
	private BigDecimal curPrice;
	private int curQty;
	private String code;
	private Time time;
	
	public StockPriceSaleAndBuy(String code) {
		this.code = code;
		deal();
	}
	
	public void deal(){
		TimeStock ts = LoadData.getTimeStock(code);
		if(time!=null&&time.before(ts.getTime())&&(ts.getQty()-curQty)!=0){
			DealedPoint dealedPoint = new DealedPoint();
			dealedPoint.setPrice(ts.getCurPrice());
			dealedPoint.setDealTime(ts.getTime());
			dealedPoint.setQty(ts.getQty()-curQty);
			if(ts.getCurPrice().compareTo(curPrice)>0){
				dealedPoint.setBuy(true);
			}else if(ts.getCurPrice().compareTo(curPrice)==0){
				dealedPoint.setBuy(ts.getCurPrice().compareTo(ts.getFirstBuyPrice())>0);
			}else{
				dealedPoint.setBuy(false);
			}
			synchronized (listDealed) {
				listDealed.add(dealedPoint);
			}
			
		}
		
		loadNotDeal(ts);
	}
	
	private void loadNotDeal(TimeStock ts){
		listNotDeal.put(ts.getFifthSalePrice(), ts.getFifthSaleqty());
		listNotDeal.put(ts.getForthSalePrice(), ts.getForthSaleqty());
		listNotDeal.put(ts.getThirdSalePrice(), ts.getThirdSaleqty());
		listNotDeal.put(ts.getSecondSalePrice(), ts.getSecondSaleqty());
		listNotDeal.put(ts.getFirstSalePrice(), ts.getFirstSaleqty());
		listNotDeal.put(ts.getFifthBuyPrice(), ts.getFifthBuyqty());
		listNotDeal.put(ts.getForthBuyPrice(), ts.getForthBuyqty());
		listNotDeal.put(ts.getThirdBuyPrice(), ts.getThirdBuyqty());
		listNotDeal.put(ts.getSecondBuyPrice(), ts.getSecondBuyqty());
		listNotDeal.put(ts.getFirstBuyPrice(), ts.getFirstBuyqty());
		curPrice=ts.getCurPrice();
		curQty = ts.getQty();
		time = ts.getTime();
		
	}
	
	public class DealedPoint{
		BigDecimal price ;
		int qty;
		Time dealTime;
		boolean isBuy;
		public BigDecimal getPrice() {
			return price;
		}
		public void setPrice(BigDecimal price) {
			this.price = price;
		}
		public int getQty() {
			return qty;
		}
		public void setQty(int qty) {
			this.qty = qty;
		}
		public Time getDealTime() {
			return dealTime;
		}
		public void setDealTime(Time dealTime) {
			this.dealTime = dealTime;
		}
		public boolean isBuy() {
			return isBuy;
		}
		public void setBuy(boolean isBuy) {
			this.isBuy = isBuy;
		}
		
	}

	public Map<BigDecimal, Integer> getListNotDeal() {
		return listNotDeal;
	}

	public List<DealedPoint> getListDealed() {
		return listDealed;
	}

	public void setListDealed(List<DealedPoint> listDealed) {
		this.listDealed = listDealed;
	}

	public BigDecimal getCurPrice() {
		return curPrice;
	}

	public void setCurPrice(BigDecimal curPrice) {
		this.curPrice = curPrice;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Time getTime() {
		return time;
	}

	public void setTime(Time time) {
		this.time = time;
	}
}
